<template>
    <div class="interest-list-container">
        <!-- 新增属性按钮 -->
        <div class="add-section">
            <el-button type="success" @click="handleAddInterest">新增兴趣</el-button>
        </div>

        <!-- 数据表格区域 -->
        <div class="table-section">
            <el-card shadow="never" class="table-card">
                <el-table 
                    :data="tableData" 
                    v-loading="loading"
                    stripe
                    border
                    style="width: 100%"
                >
                    <el-table-column type="index" label="序号" width="80" align="center" />
                    <el-table-column prop="attributeId" label="属性id" width="100" align="center" />
                    <el-table-column prop="userId" label="用户ID" width="100" align="center" />
                    <el-table-column prop="userAccount" label="用户账号" />
                    <el-table-column prop="interestName" label="兴趣名称" />
                    <el-table-column prop="createTime" label="创建时间" />
                    <el-table-column label="操作" width="200" align="center" fixed="right">
                        <template #default="{ row }">
                            <el-button type="primary" size="small" @click="handleEdit(row)">编辑</el-button>
                            <el-button type="danger" size="small" @click="handleDelete(row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-card>
        </div>

        <!-- 分页区域 -->
        <div class="pagination-section">
            <el-card shadow="never" class="pagination-card">
                <div class="pagination-content">
                    <div class="pagination-info">
                        <span>总共{{ pagination.total }}条记录,当前在第{{ pagination.current }}页</span>
                    </div>
                    <el-pagination
                        v-model:current-page="pagination.current"
                        :page-size="pagination.size"
                        :total="pagination.total"
                        :page-sizes="[10, 20, 50, 100]"
                        layout="prev, pager, next, jumper"
                        @current-change="handlePageChange"
                        @size-change="handleSizeChange"
                    />
                </div>
            </el-card>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus';

const router = useRouter();

// 表格数据
const loading = ref(false);
const tableData = ref([
    {
        attributeId: '001',
        userId: '100',
        userAccount: 'admin123',
        interestName: '兴趣名称1',
        createTime: '2020-08-08 08:08'
    },
    {
        attributeId: '002',
        userId: '111',
        userAccount: '15578585859',
        interestName: '兴趣名称2',
        createTime: '2020-08-08 08:08'
    },
    {
        attributeId: '003',
        userId: '112',
        userAccount: 'user001',
        interestName: '兴趣名称3',
        createTime: '2020-08-08 08:08'
    },
    {
        attributeId: '004',
        userId: '113',
        userAccount: 'user002',
        interestName: '兴趣名称4',
        createTime: '2020-08-08 08:08'
    },
    {
        attributeId: '005',
        userId: '114',
        userAccount: 'user003',
        interestName: '兴趣名称5',
        createTime: '2020-08-08 08:08'
    },
    {
        attributeId: '006',
        userId: '115',
        userAccount: 'user004',
        interestName: '兴趣名称6',
        createTime: '2020-08-08 08:08'
    },
    {
        attributeId: '007',
        userId: '116',
        userAccount: 'user005',
        interestName: '兴趣名称7',
        createTime: '2020-08-08 08:08'
    },
    {
        attributeId: '008',
        userId: '117',
        userAccount: 'user006',
        interestName: '兴趣名称8',
        createTime: '2020-08-08 08:08'
    },
    {
        attributeId: '009',
        userId: '118',
        userAccount: 'user007',
        interestName: '兴趣名称9',
        createTime: '2020-08-08 08:08'
    },
    {
        attributeId: '010',
        userId: '119',
        userAccount: 'user008',
        interestName: '兴趣名称10',
        createTime: '2020-08-08 08:08'
    }
]);

// 分页数据
const pagination = reactive({
    current: 1,
    size: 10,
    total: 100
});

// 分页方法
const handlePageChange = (page: number) => {
    pagination.current = page;
    loading.value = true;
    // 模拟分页请求
    setTimeout(() => {
        loading.value = false;
    }, 500);
};

const handleSizeChange = (size: number) => {
    pagination.size = size;
    pagination.current = 1;
    loading.value = true;
    // 模拟分页请求
    setTimeout(() => {
        loading.value = false;
    }, 500);
};

// 新增兴趣
const handleAddInterest = () => {
    router.push('/interest-form');
};

// 编辑兴趣
const handleEdit = (row: any) => {
    router.push(`/interest-form/${row.attributeId}`);
};

// 删除兴趣
const handleDelete = (row: any) => {
    ElMessageBox.confirm(
        `确定要删除兴趣 "${row.interestName}" 吗？`,
        '删除确认',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(() => {
        ElMessage.success('删除成功');
        // 这里可以调用删除API
    }).catch(() => {
        ElMessage.info('已取消删除');
    });
};

// 页面初始化
onMounted(() => {
    // 可以在这里加载初始数据
});
</script>

<style scoped>
.interest-list-container {
    padding: 20px;
}

/* 新增按钮区域 */
.add-section {
    margin-bottom: 20px;
}

/* 表格区域 */
.table-section {
    margin-bottom: 20px;
}

.table-card {
    border-radius: 8px;
}

/* 分页区域 */
.pagination-section {
    margin-bottom: 20px;
}

.pagination-card {
    border-radius: 8px;
}

.pagination-content {
    display: flex;
    flex-direction: row;
    justify-content: flex-end;
    gap: 20px;
    align-items: center;
}

.pagination-info {
    color: #606266;
    font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 768px) {
    .interest-list-container {
        padding: 10px;
    }
    
    .pagination-content {
        flex-direction: column;
        gap: 15px;
        align-items: center;
    }
}
</style>
